


part of appcommon;

/// 小部件扩展
extension WidgetExtension on Widget {
  Widget sizedBox({double? height, double? width}) {
    return SizedBox(height: height, width: width, child: this);
  }

  Widget opacity(double value){
    return Opacity(
      opacity: value,
      child: this
    );
  }

  Widget clipRRect({BorderRadiusGeometry radius = BorderRadius.zero}){
    return ClipRRect(
      borderRadius: radius,
      child: this,
    );
  }

  Widget intervalButton({VoidCallback? onTap, int timer = 2}){
    return CustomIntervalButton(timerInterval: timer,
      onTap: onTap, child: this);
  }

  Widget container({Decoration? decoration, Color? color,
    double? width, double? height, EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? margin, AlignmentGeometry? alignment,
    BoxConstraints? constraints}) {
    assert(color == null || decoration == null, "color and decoration can not have value on same time");
    return Container(
      alignment: alignment,
      padding: padding,
      margin: margin,
      height: height,
      width: width,
      color: color,
      decoration: decoration,
      constraints: constraints,
      child: this
    );
  }

  Widget nullWidget<T>(T? value, {Widget placeHolder = const SizedBox(),
    bool Function(T)? predict}){
    return NullWidget<T>(value,
        builder: (_, __) {
          return this;
        },
        predict: predict,
        placeHolder: placeHolder);
  }

  // Widget selectorCustom<T extends ChangeNotifier, T1>({required T1 Function(T) selector,
  //   ShouldRebuild<T1>? shouldRebuild}){
  //   print(2222222222);
  //   return SelectorCustom<T, T1>(builder: (_) {
  //     print(33333333333333);
  //     return (()=> this)();
  //   }, selector: selector,
  //       shouldRebuild: shouldRebuild);
  // }

  Widget gestureDetector({void Function()? onTap, void Function()? onLongPress}){
    return GestureDetector(
      onTap: onTap,
      onLongPress: onLongPress,
      child: this
    );
  }

  Widget inkWell({void Function()? onTap}){
    return InkWell(
      onTap: onTap,
      child: this
    );
  }

  Widget safeArea({bool left = true, bool top = true, bool right = true,
    bool bottom = true, EdgeInsets minimum = EdgeInsets.zero,
    bool maintainBottomViewPadding = false}){
    return SafeArea(
      left: left,
      right: right,
      top: top,
      bottom: bottom,
      minimum: minimum,
      maintainBottomViewPadding: maintainBottomViewPadding,
      child: this,
    );
  }

  Widget get center => Center(child: this);

  Widget position({double? left, double? top, double? right, double? bottom,
    double? width, double? height}){
    return Positioned(
      left: left,
      right: right,
      top: top,
      bottom: bottom,
      width: width,
      height: height,
      child: this
    );
  }

  Widget positionFill({double? left, double? top, double? right, double? bottom}){
    return Positioned.fill(
      left: left,
      right: right,
      top: top,
      bottom: bottom,
      child: this
    );
  }


  Widget padding({required EdgeInsetsGeometry padding}){
    return Padding(
      padding: padding,
      child: this,
    );
  }

  Widget expanded({int flex = 1}){
    return Expanded(
      flex: flex,
      child: this,
    );
  }

  Widget get flexible => Flexible(child: this);

  SliverToBoxAdapter get sliverToBoxAdapter => SliverToBoxAdapter(child: this);

  Widget singleScrollView({ScrollController? controller, ScrollPhysics? physics,
    Axis scrollDirection = Axis.vertical}){
    return SingleChildScrollView(
      scrollDirection: scrollDirection,
      physics: physics,
      controller: controller,
      child: this
    );
  }

  // Widget visibleSingleScrollView({
  //   CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  //   MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  //   MainAxisSize mainAxisSize = MainAxisSize.max,
  //   TextBaseline? textBaseline,
  //   TextDirection? textDirection,
  //   bool needBounds = true,
  //   VerticalDirection verticalDirection = VerticalDirection.down,
  //   ScrollPhysics? scrollPhysics}){
  //   return VisibleSingleScrollView(
  //     crossAxisAlignment: crossAxisAlignment,
  //     mainAxisAlignment: mainAxisAlignment,
  //     scrollPhysics: scrollPhysics,
  //     mainAxisSize: mainAxisSize,
  //     textBaseline: textBaseline,
  //     needBounds: needBounds,
  //     textDirection: textDirection,
  //     verticalDirection: verticalDirection,
  //     children: [
  //       this
  //     ],
  //   );
  // }

  // Widget sampleVisibleEnsure(FocusNode node){
  //   return SampleVisibleEnsure(node, child: this);
  // }

  Widget get ignorePointer => IgnorePointer(child: this);

  Widget get offstage => Offstage(offstage: true, child: this);

  Widget cupertinoButton({VoidCallback? onTap,
    EdgeInsetsGeometry padding = EdgeInsets.zero,
    double? miniSize}) {
    return CupertinoButton(
      onPressed: onTap,
      padding: padding,
      minSize: miniSize,
      child: this
    );
  }

  Widget transformScaleX(double scale){
    return Transform(
      transform: Matrix4.diagonal3Values(scale, 1.0, 1.0),
      child: this
    );
  }

  Widget transformScaleY(double scale){
    return Transform(
        transform: Matrix4.diagonal3Values(1.0, scale, 1.0),
        child: this
    );
  }

  Widget transformScale(double scale){
    return Transform.scale(
        scale: scale,
        child: this
    );
  }
  // Widget get observe => g.Obx(()=> this);
}

extension ListExtension on List<Widget> {
  Widget customScrollView({ScrollController? controller,
    ScrollPhysics? physics}){
    return CustomScrollView(
      physics: physics,
      controller: controller,
      slivers: this
    );
  }

  // Widget refreshWidget({ScrollController? scrollController, Axis? scrollDirection,
  //   ScrollPhysics? physics, void Function(RefreshController)? onLoading,
  //   void Function(RefreshController)? onRefresh,
  //   bool enablePullDown = true, bool enablePullUp = false}){
  //   return RefreshWidget(
  //     scrollController: scrollController,
  //     scrollDirection: scrollDirection,
  //     physics: physics,
  //     onLoading: onLoading,
  //     onRefresh: onRefresh,
  //     enablePullDown: enablePullDown,
  //     enablePullUp: enablePullUp,
  //     children: this
  //   );
  // }

  Widget row({MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection? textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down}){
    return Row(
      mainAxisSize: mainAxisSize,
      mainAxisAlignment: mainAxisAlignment,
      crossAxisAlignment: crossAxisAlignment,
      textDirection: textDirection,
      verticalDirection: verticalDirection,
      children: this,
    );
  }

  Widget column({MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection? textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down}){
    return Column(
      mainAxisSize: mainAxisSize,
      mainAxisAlignment: mainAxisAlignment,
      crossAxisAlignment: crossAxisAlignment,
      textDirection: textDirection,
      verticalDirection: verticalDirection,
      children: this
    );
  }

  // Widget visibleSingleScrollView({
  //   CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  //   MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  //   MainAxisSize mainAxisSize = MainAxisSize.max,
  //   TextBaseline? textBaseline,
  //   TextDirection? textDirection,
  //   bool needBounds = true,
  //   VerticalDirection verticalDirection = VerticalDirection.down,
  //   ScrollPhysics? scrollPhysics}){
  //   return VisibleSingleScrollView(
  //     crossAxisAlignment: crossAxisAlignment,
  //     mainAxisAlignment: mainAxisAlignment,
  //     scrollPhysics: scrollPhysics,
  //     mainAxisSize: mainAxisSize,
  //     needBounds: needBounds,
  //     textBaseline: textBaseline,
  //     textDirection: textDirection,
  //     verticalDirection: verticalDirection,
  //     children: this,
  //   );
  // }
}